package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.jinjimi.app.R;

/**
 * Created by lforxeverc on 2017/7/21.
 */

public class ModuleRetryView extends FrameLayout implements View.OnClickListener {

    private TextView tvRetry;
    private ProgressBar pbLoading;
    private View vRetry;
    private String mModuleName;

    private OnRetryListener mListener;

    public ModuleRetryView(Context context) {
        this(context, null);
    }

    public ModuleRetryView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModuleRetryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.view_module_retry, this);
        vRetry = findViewById(R.id.v_retry);
        tvRetry = (TextView) findViewById(R.id.btn_retry);
        pbLoading = (ProgressBar) findViewById(R.id.progress);

        pbLoading.setVisibility(GONE);

        tvRetry.setOnClickListener(this);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ModuleRetryView);
        mModuleName = typedArray.getString(R.styleable.ModuleRetryView_module_name);
        typedArray.recycle();
    }

    @Override
    public void onClick(View v) {
        pbLoading.setVisibility(VISIBLE);
        vRetry.setVisibility(GONE);
        if (mListener != null) {
            mListener.onRetry(mModuleName);
        }
    }

    public void onFail() {
        setVisibility(VISIBLE);
        pbLoading.setVisibility(GONE);
        vRetry.setVisibility(VISIBLE);
    }

    public void onSuccess() {
        setVisibility(GONE);
    }

    public void setOnRetryListener(OnRetryListener mListener) {
        this.mListener = mListener;
    }

    public String getModuleName() {
        return mModuleName;
    }

    public interface OnRetryListener {
        void onRetry(String moduleName);
    }
}
